package com.wyp168.socket.tcp.socketDemo01;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class Client  {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream outputStream = null;
        try {
            //链接服务器(ip, 端口), 如果连接成功，返回socket对象
            socket = new Socket(InetAddress.getLocalHost(), 9999);
            System.out.println("客户端 socket: " + socket.getClass());
            //连接上后，生成Socket， 通过socket.getOutputStream() 得到和socket 对象关联的输出流对象
            outputStream = socket.getOutputStream();
            // 通过输出流，写入数据到数据通道
            outputStream.write("hello, server".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭流对象和socket资源类
                if (outputStream != null) {
                    //关闭流对象资源
                    outputStream.close();
                    System.out.println("客户端退出");
                }
                if (socket != null) {
                    socket.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }






    }
}
